﻿using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class PageVision : Form
    {
        public PageVision()
        {
            InitializeComponent();
        }

        private void label189_Click(object sender, EventArgs e)
        {

        }
        private e_CameraPlate GetSelect_Camera()
        {
            var res = e_CameraPlate.定位相机平台1;
            if (rbn_v_Plate1.Checked)
                res= e_CameraPlate.定位相机平台1;
           else if (rbn_v_Plate2.Checked)
                res= e_CameraPlate.定位相机平台2;
            return res;
        }
        private e_platePro GetSelect_Plate()
        {
            var res = e_platePro.平台1;
            if (rbn_v_Plate1.Checked)
                res = e_platePro.平台1;
            else if (rbn_v_Plate2.Checked)
                res = e_platePro.平台2;
            return res;
        }
        private void btn_Label(object sender, EventArgs e)
        {
            displalyInfo.displayInfoMain(((Button)sender).Text + "按钮按下");
            if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.superAdmin)
            {
                var arr_step = Enum.GetNames(typeof(e_Step_Label));
                if (arr_step.Contains(((Button)sender).Text))
                {
                    var e_step = (e_Step_Label)Enum.Parse(typeof(e_Step_Label), ((Button)sender).Text);
                    var curPlate = GetSelect_Plate();
                    //if(e_step==e_Step_Label.移至拍照安全位X)
                    //{
                    //    if (curPlate == e_platePro.平台1) curPlate = e_platePro.平台2;
                    //    if (curPlate == e_platePro.平台2) curPlate = e_platePro.平台1;

                    //}
                    GlobalMotion.Step_Labeling(e_step, curPlate, e_MachineStatus.无控制);
                }
                else
                {
                    MessageBox.Show($"请注册{((Button)sender).Text }按钮功能");
                }
            }
            else
            {
                displalyInfo.displayInfoMain(((Button)sender).Text + "按钮权限不足");
            }
        }
        bool isFirst = true;
        private void PageVision_Load(object sender, EventArgs e)
        {
            if(isFirst)
            {
                isFirst = false;
                rbn_v_Plate1.Checked = true;
                GlobalVar.imageDisplay1 = ImageDisplay_1;
                GlobalVar.imageDisplay1_obj = ImageDisplay_1_obj;
                GlobalVar.Image_drawRoiRegion1 = DrawRoiRegion1;
                GlobalVar.Image_clearRoiRegion1 = ClearRoiRegion1;
                rbn_v_Plate1.CheckedChanged += cbx_Region_SelectedIndexChanged;
                LoadParamFromType(GlobalVar.closeSave.CurrentTypeName);
                GlobalVar.ChangeModel += LoadParamFromType;
                GlobalVar.RefreshModel += FreshVersionType;
                GlobalVar.DisplayResult_cur = display_Result;
            }
        }
        private void display_Result()
        {
            this.BeginInvoke(new Action(() => {
                var msg = "";
                if (GlobalVar.e_Camera_Pos1 == e_Camera_Pos.已拍照OK && visionProc.ResultPlate1 != null)
                {
                    var str_1 = e_CameraPlate.定位相机平台1.ToString() +
              $":X:{visionProc.ResultPlate1.X.ToString("F02")}Y:{visionProc.ResultPlate1.Y.ToString("F02")},Angel:{visionProc.ResultPlate1.Angle.ToString("F02")}";
                    msg += str_1;
                }
                if (GlobalVar.e_Camera_Pos2 == e_Camera_Pos.已拍照OK && visionProc.ResultPlate2 != null)
                {
                    var str_2 = e_CameraPlate.定位相机平台2.ToString() +
                $":X:{visionProc.ResultPlate2.X.ToString("F02")}Y:{visionProc.ResultPlate2.Y.ToString("F02")},Angel:{visionProc.ResultPlate2.Angle.ToString("F02")}";
                    msg += str_2;
                }
                rbt_XYZ.Text = msg; 
            }));
         
        }
        private void Rbn_v_Plate1_CheckedChanged(object sender, EventArgs e)
        {
            //平台参数
           
        }

       
        private int LoadParamFromType(string typeVersion)
        {
            try
            {
                this.cbo_type_selectType.SelectedIndexChanged -= new System.EventHandler(this.cbo_selectVersion_SelectedIndexChanged);
                cbo_selectVersion.Items.Clear();
                cbo_type_selectType.Items.Clear();
                if(GlobalVar.tv_list.Count>0)
                {
                    cbo_type_selectType.Items.AddRange(GlobalVar.tv_list.Select(A => A.type).Distinct().ToArray());
                    if (typeVersion.Contains(GlobalVar.char_VersionSpilt))
                    {
                        var arr = typeVersion.Split(GlobalVar.char_VersionSpilt);
                        var arrVersion= GlobalVar.tv_list.Where(A => A.type == arr[0]).Select(A => A.Version).Distinct().ToArray();
                        cbo_selectVersion.Items.AddRange(arrVersion);
                        cbo_type_selectType.Text = arr[0];
                        cbo_selectVersion.Text = arr[1];
                        ParamaToControl(GlobalVar.motionConfig);
                        visionProc.ReadTemplateRegion();
                        visionProc.ReadRegion();
                    }
                }
                this.cbo_type_selectType.SelectedIndexChanged += new System.EventHandler(this.cbo_selectVersion_SelectedIndexChanged);


            }
            catch(Exception ex)
            {
                return -1;
            }
         
            return 1;
        }
        private int FreshVersionType(string typeVersion)
        {
            try
            {
                typeVersion = GlobalVar.closeSave.CurrentTypeName;

                this.cbo_type_selectType.SelectedIndexChanged -= new System.EventHandler(this.cbo_selectVersion_SelectedIndexChanged);
                cbo_selectVersion.Items.Clear();
                cbo_type_selectType.Items.Clear();
                if (GlobalVar.tv_list.Count > 0)
                {
                    cbo_type_selectType.Items.AddRange(GlobalVar.tv_list.Select(A => A.type).Distinct().ToArray());
                    if (typeVersion.Contains(GlobalVar.char_VersionSpilt))
                    {
                        var arr = typeVersion.Split(GlobalVar.char_VersionSpilt);
                        var arrVersion = GlobalVar.tv_list.Where(A => A.type == arr[0]).Select(A => A.Version).Distinct().ToArray();
                        cbo_selectVersion.Items.AddRange(arrVersion);
                        cbo_type_selectType.Text = arr[0];
                        cbo_selectVersion.Text = arr[1];
                    }
                }
                this.cbo_type_selectType.SelectedIndexChanged += new System.EventHandler(this.cbo_selectVersion_SelectedIndexChanged);


            }
            catch (Exception ex)
            {
                return -1;
            }

            return 1;
        }
        private void ParamaToControl(MotionConfig motionConfig)
        {
            if (motionConfig.visionBase1 == null)
                motionConfig.visionBase1 = new VisionPos();
            if (motionConfig.visionBase2 == null)
                motionConfig.visionBase2 = new VisionPos();
            #region 定位相机
            var curSelect = GetSelect_Camera();
            if (motionConfig.TC_list == null)
                motionConfig.TC_list = new List<TemplateParam>();
            var tcount1 = Enum.GetNames(typeof(e_CameraPlate)).Length;
            for (int i = 0; i < tcount1; i++)
            {
                motionConfig.TC_list.Add(new TemplateParam());
            }
            if (motionConfig.TF_list == null)
                motionConfig.TF_list = new List<TemplateFindParam>();
            var tcount2 = Enum.GetNames(typeof(e_CameraPlate)).Length;
            for (int i = 0; i < tcount2; i++)
            {
                motionConfig.TF_list.Add(new TemplateFindParam());
            }
            set_TemplateFind(motionConfig.TF_list[(int)curSelect]);
            set_TemplateCreate(motionConfig.TC_list[(int)curSelect]);
            if (curSelect == e_CameraPlate.定位相机平台1)
            {
                set_VisionCenter(motionConfig.visionCenter1);
                set_VisionPut(motionConfig.visionPutOK1);
                set_VisionBase(motionConfig.visionBase1);
            }
            else if (curSelect == e_CameraPlate.定位相机平台2)
            {
                set_VisionCenter(motionConfig.visionCenter2);
                set_VisionPut(motionConfig.visionPutOK2);
                set_VisionBase(motionConfig.visionBase2);
            }
            #endregion
            #region 定位直线
            if (motionConfig.PosLindFind1 == null)
            {
                motionConfig.PosLindFind1 = new List<LindFindParam>();
            }
            if (motionConfig.PosLindFind2 == null)
            {
                motionConfig.PosLindFind2 = new List<LindFindParam>();
            }
            var LCount = (int)e_Region.直线区域2 - (int)e_Region.直线区域1 + 1;
            for (int i = motionConfig.PosLindFind1.Count; i < LCount; i++)
            {
                motionConfig.PosLindFind1.Add(new LindFindParam());
            }
            for (int i = motionConfig.PosLindFind2.Count; i < LCount; i++)
            {
                motionConfig.PosLindFind2.Add(new LindFindParam());
            }

            if (cbx_Region.SelectedIndex >= (int)e_Region.直线区域1 && cbx_Region.SelectedIndex <= (int)e_Region.直线区域2)
            {
                if (curSelect == e_CameraPlate.定位相机平台1)
                {
                    var lineParama = motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                    Set_LindFindParam(lineParama);
                }
                else if (curSelect == e_CameraPlate.定位相机平台2)
                {
                    var lineParama = motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                    Set_LindFindParam(lineParama);
                }
            }
            #endregion
            rtb_OKdirSelect.Text = motionConfig.dir_PosOK;
            rtb_NGdirSelect.Text = motionConfig.dir_PosNG;
        }
       
        private void SaveParamaToControl(MotionConfig motionConfig)
        {
          
            #region 模板定位
            if (motionConfig.visionBase1 == null)
                motionConfig.visionBase1 = new VisionPos();
            if (motionConfig.visionBase2 == null)
                motionConfig.visionBase2 = new VisionPos();
            if (motionConfig.TC_list == null)
            {
                motionConfig.TC_list = new List<TemplateParam>();
            }
            var tcount1 = Enum.GetNames(typeof(e_CameraPlate)).Length;
            for (int i = motionConfig.TC_list.Count; i < tcount1; i++)
            {
                motionConfig.TC_list.Add(new TemplateParam());
            }
            if (motionConfig.TF_list == null)
            {
                motionConfig.TF_list = new List<TemplateFindParam>();
            }

            var tcount2 = Enum.GetNames(typeof(e_CameraPlate)).Length;
            for (int i = motionConfig.TF_list.Count; i < tcount2; i++)
            {
                motionConfig.TF_list.Add(new TemplateFindParam());
            }
            var curSelect = GetSelect_Camera();
            if (curSelect == e_CameraPlate.定位相机平台1)
            {
                motionConfig.visionCenter1 = Get_VisionCenter();
                motionConfig.visionPutOK1 = Get_VisionPut();
                motionConfig.visionBase1 = Get_VisionBase();
            }
            else if (curSelect == e_CameraPlate.定位相机平台2)
            {
                motionConfig.visionCenter2 = Get_VisionCenter();
                motionConfig.visionBase2 = Get_VisionBase();
                motionConfig.visionPutOK2 = Get_VisionPut();
            }
            #endregion
            #region 直线定位
            if (motionConfig.PosLindFind1 == null)
            {
                motionConfig.PosLindFind1 = new List<LindFindParam>();
            }
            if (motionConfig.PosLindFind2 == null)
            {
                motionConfig.PosLindFind2 = new List<LindFindParam>();
            }

            var LCount = (int)e_Region.直线区域2 - (int)e_Region.直线区域1 + 1;
            for (int i = motionConfig.PosLindFind1.Count; i < LCount; i++)
            {
                motionConfig.PosLindFind1.Add(new LindFindParam());
            }
            for (int i = motionConfig.PosLindFind2.Count; i < LCount; i++)
            {
                motionConfig.PosLindFind2.Add(new LindFindParam());
            }

            if (cbx_Region.SelectedIndex >= (int)e_Region.直线区域1 && cbx_Region.SelectedIndex <= (int)e_Region.直线区域2)
            {
                if (curSelect == e_CameraPlate.定位相机平台1)
                {
                    motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1] = Get_LindFindParam(GlobalVar.motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1]);
                }
                else if (curSelect == e_CameraPlate.定位相机平台2)
                {
                    motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1] = Get_LindFindParam(GlobalVar.motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1]);
                }
            }
            #endregion
            motionConfig.TF_list[(int)curSelect] = Get_TemplateFind();
            motionConfig.TC_list[(int)curSelect] = Get_TemplateCreate();
            motionConfig.dir_PosOK = rtb_OKdirSelect.Text;
            motionConfig.dir_PosNG = rtb_NGdirSelect.Text;
            if (!Directory.Exists(GlobalVar.type_path))
            {
                Directory.CreateDirectory(GlobalVar.type_path);
            }
            if (!Directory.Exists(GlobalVar.type_copy_path))
            {
                Directory.CreateDirectory(GlobalVar.type_copy_path);
            }
            SerializeHelper.serializeFile(GlobalVar.type_path + "\\" + GlobalVar.closeSave.CurrentTypeName + ".tfp", motionConfig, GlobalVar.closeSave.CurrentTypeName);
            SerializeHelper.serializeFile(GlobalVar.type_copy_path + "\\" + GlobalVar.closeSave.CurrentTypeName + ".tfp", motionConfig, GlobalVar.closeSave.CurrentTypeName);
        }
        private void set_VisionCenter(VisionPos put)
        {
            if (put == null)
                put = new VisionPos();
            nud_VisionCenterX.Value = (decimal)put.X;
            nud_VisionCenterY.Value = (decimal)put.Y;
            nud_VisionCenterR.Value = (decimal)put.R;
        }
        private void btn_ReadTemplateImage_Click(object sender, EventArgs e)
        {
            var curCameraPlate = GetSelect_Camera();
            var Path_tempImage = Application.StartupPath + "\\TemplateImage\\" + GlobalVar.closeSave.CurrentTypeName + "\\" + curCameraPlate.ToString() + ".jpg";
           if(File.Exists(Path_tempImage))
            {
                var temp= visionProc.ReadImage(Path_tempImage);
                if(temp!=null)
                visionProc.srcObject_pos= temp;
                GlobalVar.imageDisplay1(visionProc.srcObject_pos);
            }
        }

        private void btn_CameraSnap_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_Camera();
            visionProc.TemplateParam_Find = Get_TemplateFind();
            visionProc.ReadRegion();
            switch (curSelect)
            {
                case e_CameraPlate.定位相机平台1:
                case e_CameraPlate.定位相机平台2:
                    visionProc.curCameraPlate1 = curSelect;
                    GlobalVar.cameraList[(int)e_Camera.定位相机].softTrigger();
                    break;

            }
        }

        private void btn_ReadImage_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "All File(*.*)|*.*";
                ofd.Title = "选择打印模板文件";
                ofd.InitialDirectory = Application.StartupPath;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    var temp = visionProc.ReadImage(ofd.FileName);
                    visionProc.srcObject_pos = temp;
                    GlobalVar.imageDisplay1(visionProc.srcObject_pos);
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain($"{((Button)sender).Text}-[error] {ex.Message}");
            }
        }

        private void btn_Draw_Click(object sender, EventArgs e)
        {
            GlobalVar.Image_drawRoiRegion1();
        }

        private void btn_clearRoi_Click(object sender, EventArgs e)
        {
            GlobalVar.Image_clearRoiRegion1();
            MaskRegion1 = new HObject();
        }

        private void cbx_Region_SelectedIndexChanged(object sender, EventArgs e)
        {
            var curMotionConfig = GlobalVar.motionConfig;
            var curPlate = GetSelect_Camera();
            set_TemplateFind(curMotionConfig.TF_list[(int)curPlate]);
            set_TemplateCreate(curMotionConfig.TC_list[(int)curPlate]);
            if(curPlate==e_CameraPlate.定位相机平台1)
            {
                set_VisionPut(curMotionConfig.visionPutOK1);
                set_VisionBase(curMotionConfig.visionBase1);
                set_VisionCenter(curMotionConfig.visionCenter1);
            }
            else if (curPlate == e_CameraPlate.定位相机平台2)
            {
                set_VisionPut(curMotionConfig.visionPutOK2);
                set_VisionBase(curMotionConfig.visionBase2);
                set_VisionCenter(curMotionConfig.visionCenter2);
            }
            if (cbx_Region.SelectedIndex >= (int)e_Region.直线区域1 && cbx_Region.SelectedIndex <= (int)e_Region.直线区域2)
            {

                if (curPlate == e_CameraPlate.定位相机平台1)
                {
                    if (GlobalVar.motionConfig.PosLindFind1 != null && GlobalVar.motionConfig.PosLindFind1.Count >= (cbx_Region.SelectedIndex - (int)e_Region.直线区域1))
                    {
                        var lineParama = GlobalVar.motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                        Set_LindFindParam(lineParama);
                    }
                }
                else if (curPlate == e_CameraPlate.定位相机平台2)
                {
                    if (GlobalVar.motionConfig.PosLindFind2 != null && GlobalVar.motionConfig.PosLindFind2.Count >= (cbx_Region.SelectedIndex - (int)e_Region.直线区域1))
                    {
                        var lineParama = GlobalVar.motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                        Set_LindFindParam(lineParama);
                    }
                }
            }
        }
        private LindFindParam Get_LindFindParam(LindFindParam Lparam)
        {
            if (Lparam == null)
                Lparam = new LindFindParam();
            Lparam.dLength = (double)nud_tL_RulerLength.Value;
            Lparam.dSigma = (double)nud_tL_Sigma.Value;
            Lparam.dWidth = (double)nud_tL_RulerWidth.Value;
            Lparam.Threshold = (int)nud_tL_thread.Value;
            return Lparam;
        }
        private LindFindParam Set_LindFindParam(LindFindParam param)
        {
            if (param == null)
                param = new LindFindParam();
            nud_tL_RulerLength.Value = (decimal)param.dLength;
            nud_tL_Sigma.Value = (decimal)param.dSigma;
            nud_tL_RulerWidth.Value = (decimal)param.dWidth;
            nud_tL_thread.Value = (decimal)param.Threshold;
            return param;
        }
        private void btn_OKDir_Select_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofg = new FolderBrowserDialog();
            ofg.Description = "选择文件夹";
            ofg.SelectedPath = Environment.CurrentDirectory;
            var res = ofg.ShowDialog();
            if (res == DialogResult.OK)
            {
                string paths = ofg.SelectedPath;
                rtb_OKdirSelect.Text = paths;
            }
        }

        private void btn_NGdirSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofg = new FolderBrowserDialog();
            ofg.Description = "选择文件夹";
            ofg.SelectedPath = Environment.CurrentDirectory;
            var res = ofg.ShowDialog();
            if (res == DialogResult.OK)
            {
                string paths = ofg.SelectedPath;
                rtb_NGdirSelect.Text = paths;
            }
        }

        private void btn_ReadROI_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_Camera();
            visionProc.ReadRegion();
            if (curSelect == e_CameraPlate.定位相机平台1 || curSelect == e_CameraPlate.定位相机平台2)
            {
                visionProc.ROI1 = visionProc.ROI_List[(int)curSelect];
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Region, "green");
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Template, "blue");
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Postion, "orange");
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Postion2, "orange");
        
            }
          
        }

        private void btn_SaveROI_Click(object sender, EventArgs e)
        {
            var nRegion = cbx_Region.Items.IndexOf(cbx_Region.Text);
            if (nRegion > -1)
            {
                var arr = Enum.GetNames(typeof(e_Region));
                if (arr.Contains(cbx_Region.Text))
                {
                    var curSelect = GetSelect_Camera();
                    //if(rbn_v_plate1.Checked)
                    visionProc.SaveRegion((e_Region)Enum.Parse(typeof(e_Region), cbx_Region.Text), curSelect);
                    if (nRegion >= (int)e_Region.直线区域1)
                    {
                        if (curSelect == e_CameraPlate.定位相机平台1)
                        {
                            var curROI = visionProc.ROI_List[(int)curSelect];
                            if (curROI.ROI_Template != null && curROI.ROI_Template.CountObj() > 0)
                            {
                                if (nRegion == (int)e_Region.直线区域1)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                                if (nRegion == (int)e_Region.直线区域2)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion2, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                              
                            }
                        }
                        else if (curSelect == e_CameraPlate.定位相机平台2)
                        {
                            var curROI = visionProc.ROI_List[(int)curSelect];
                            if (curROI.ROI_Template != null && curROI.ROI_Template.CountObj() > 0)
                            {
                                if (nRegion == (int)e_Region.直线区域1)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                                if (nRegion == (int)e_Region.直线区域2)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);

                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion2, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                               
                            }
                        }
                    }
                    displalyInfo.displayInfoMain($"{curSelect.ToString()}区域：{cbx_Region.Text} 保存成功");
                    return;
                }

            }
            displalyInfo.displayErrorMain($"区域：{cbx_Region.Text}保存失败");
        }

        private void btn_CreateTemplate_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_Camera();
            var param = Get_TemplateCreate();
            if (curSelect == e_CameraPlate.定位相机平台1 || curSelect == e_CameraPlate.定位相机平台2)
                visionProc.TemplateIds[(int)curSelect] = visionProc.CreateTemplate(visionProc.srcObject_pos, visionProc.ROI1.ROI_Template, param, e_Camera.定位相机);
        }
        private TemplateParam Get_TemplateCreate()
        {
            var Template_Create = new TemplateParam();
            //Template_Create.eMetric = (e_Metric)Enum.Parse(typeof(e_Metric), cbx_tc_eMetric.Text.Trim());
            //Template_Create.eOptmization = (e_Optmization)Enum.Parse(typeof(e_Optmization), cbx_tc_eOptminzation.Text.Trim());
            Template_Create.nContras = (int)nud_tc_Contra.Value;
            Template_Create.nLevels = (int)nud_tc_nLevel.Value;
            Template_Create.nMinContrast = (int)nud_tc_MinContra.Value;
            return Template_Create;
        }
        private void set_TemplateCreate(TemplateParam Template_Create)
        {
            if (Template_Create == null)
                Template_Create = new TemplateParam();
            //cbx_tc_eMetric.Text = Template_Create.eMetric.ToString();
            //cbx_tc_eOptminzation.Text = Template_Create.eOptmization.ToString();
            nud_tc_Contra.Value = Template_Create.nContras;
            nud_tc_nLevel.Value = Template_Create.nLevels;
            nud_tc_MinContra.Value = Template_Create.nMinContrast;
        }

        private void btn_ReadTemplate_Click(object sender, EventArgs e)
        {
            visionProc.ReadTemplateRegion();
            try
            {
                var curCameraPlate = GetSelect_Camera();
                if(visionProc.Template_xlds[(int)curCameraPlate]!=null)
                {
                    GlobalVar.imageDisplay1_obj(visionProc.Template_xlds[(int)curCameraPlate], "yellow");
                }
            }catch(Exception ex)
            {

            }
        }

        private void btn_SaveTemplate_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_Camera();
            visionProc.SaveTemplateRegion(visionProc.TemplateIds[(int)curSelect], curSelect);
        }

        private void btn_reduce_Click(object sender, EventArgs e)
        {
            visionProc.isReduce = true;
            visionProc.reduce_Radius = (int)nud_radiusReduce.Value;
        }

        private void btn_reduceOK_Click(object sender, EventArgs e)
        {
            visionProc.isReduce = false;
        }
        private TemplateFindParam Get_TemplateFind()
        {
            var Template_Find = new TemplateFindParam();
            Template_Find.nLevels = (int)nud_tf_Level.Value;
            //Template_Find.dGreediness = (double)nud_tf_Greediness.Value;
            Template_Find.dMaxOverlap = (double)nud_tf_MaxOverlap.Value;
            Template_Find.dMinScore = (double)nud_tf_MinScore.Value;
            //Template_Find.nNumMatches = (int)nud_tf_NumMatch.Value;
            //Template_Find.eSubPixel = (e_SubPixel)Enum.Parse(typeof(e_SubPixel), cbx_tf_eSubPixel.Text.Trim());
            Template_Find.xMax =(double)nud_Xmax.Value;
            Template_Find.xMin = (double)nud_Xmin.Value;
            Template_Find.yMax = (double)nud_Ymax.Value;
            Template_Find.yMin = (double)nud_Ymin.Value;
            Template_Find.rMax = (double)nud_Rmax.Value;
            Template_Find.rMin = (double)nud_Rmin.Value;
            return Template_Find;
        }
        private void set_TemplateFind(TemplateFindParam Template_Find)
        {
            if (Template_Find == null)
                Template_Find = new TemplateFindParam();
            nud_tf_Level.Value = Template_Find.nLevels;
            //nud_tf_Greediness.Value = (decimal)Template_Find.dGreediness;
            nud_tf_MaxOverlap.Value = (decimal)Template_Find.dMaxOverlap;
            nud_tf_MinScore.Value = (decimal)Template_Find.dMinScore;
            //nud_tf_NumMatch.Value = (decimal)Template_Find.nNumMatches;
            //cbx_tf_eSubPixel.Text = Template_Find.eSubPixel.ToString();
            nud_Xmax.Value = (decimal)Template_Find.xMax;
            nud_Xmin.Value = (decimal)Template_Find.xMin;
            nud_Ymax.Value = (decimal)Template_Find.yMax;
            nud_Ymin.Value = (decimal)Template_Find.yMin;
            nud_Rmax.Value = (decimal)Template_Find.rMax;
            nud_Rmin.Value = (decimal)Template_Find.rMin;
        }
        private void btn_CheckTemplate_Click(object sender, EventArgs e)
        {
            var select = GetSelect_Camera();
            visionProc.TemplateParam_Find = Get_TemplateFind();
            if (select == e_CameraPlate.定位相机平台1 || select == e_CameraPlate.定位相机平台2)
            {
                var res = visionProc.findTemplate(visionProc.srcObject_pos, visionProc.TemplateIds[(int)select], visionProc.TemplateParam_Find, visionProc.ROI_List[(int)select].ROI_Region);
                if (res != null)
                {
                    HOperatorSet.GenCrossContourXld(out HObject crosing, res.dRow, res.dColumn, 200, res.dAngle);
                    GlobalVar.imageDisplay1_obj(crosing, "red");
                    displalyInfo.displayInfoMain(JsonConvert.SerializeObject(res));
                }
            }
          
        }

        private void btn_image_Click(object sender, EventArgs e)
        {
          
        }

        private void btn_displayCenter_Click(object sender, EventArgs e)
        {
            var xPix = nud_VisionCenterX.Value;
            var yPix = nud_VisionCenterY.Value;
            var curPlate = GetSelect_Plate();
            if (curPlate==e_platePro.平台1)
            {
                if (visionProc.Mark1Pix != null)
                {
                    HOperatorSet.GenCrossContourXld(out HObject CenterObj, visionProc.Mark1Pix.Y + (double)yPix, visionProc.Mark1Pix.X + (double)xPix, 200, 0);
                    GlobalVar.imageDisplay1_obj(CenterObj, "blue");
                }
            }
            else
            {
                if (visionProc.Mark2Pix != null)
                {
                    HOperatorSet.GenCrossContourXld(out HObject CenterObj, visionProc.Mark2Pix.Y + (double)yPix, visionProc.Mark2Pix.X + (double)xPix, 200, 0);
                    GlobalVar.imageDisplay1_obj(CenterObj, "blue");
                }
            }
        }

        private void btn_RefreshPut_Click(object sender, EventArgs e)
        {
            var curPlate = GetSelect_Plate();
            if (curPlate == e_platePro.平台1)
            {
                if (visionProc.realResurt1 != null)
                {
                    var temp = new VisionPos() { X = visionProc.realResurt1.X, Y = visionProc.realResurt1.Y, R = -visionProc.realResurt1.Angle };
                    set_VisionPut(temp);

                }
                if (visionProc.BasePostion1 != null)
                {
                    var temp = new VisionPos() { X = visionProc.BasePostion1.X, Y = visionProc.BasePostion1.Y, R = -visionProc.BasePostion1.Angle };
                    set_VisionBase(temp);
                }
            }
            else
            {
                if (visionProc.realResurt2 != null)
                {
                    var temp = new VisionPos() { X = visionProc.realResurt2.X, Y = visionProc.realResurt2.Y, R = -visionProc.realResurt2.Angle };
                    set_VisionPut(temp);
                }
                if (visionProc.BasePostion2 != null)
                {
                    var temp = new VisionPos() { X = visionProc.BasePostion2.X, Y = visionProc.BasePostion2.Y, R = -visionProc.BasePostion2.Angle };
                    set_VisionBase(temp);
                }
            }
        }
        private VisionPos Get_VisionPut()
        {

            var put = new VisionPos();
            put.X = (double)nud_VisionPutX.Value;
            put.Y = (double)nud_VisionPutY.Value;
            put.R = (double)nud_VisionPutR.Value;
            return put;
        }
        private void set_VisionPut(VisionPos put)
        {
            if (put == null)
                put = new VisionPos();
            nud_VisionPutX.Value = (decimal)put.X;
            nud_VisionPutY.Value = (decimal)put.Y;
            nud_VisionPutR.Value = (decimal)put.R;
        }
        private VisionPos Get_VisionBase()
        {

            var basePos = new VisionPos();
            basePos.X = (double)nud_baseX.Value;
            basePos.Y = (double)nud_baseY.Value;

            return basePos;
        }

        private void set_VisionBase(VisionPos basePos)
        {
            if (basePos == null)
                basePos = new VisionPos();
            nud_baseX.Value = (decimal)basePos.X;
            nud_baseY.Value = (decimal)basePos.Y;

        }


        private void button27_Click(object sender, EventArgs e)
        {
            visionProc.visionProcCam(visionProc.srcObject_pos, GetSelect_Camera());
        }

        private void btn_SaveConfig_Click(object sender, EventArgs e)
        {
            var curType = cbo_type_selectType.Text.Trim();
            var curVersion = cbo_selectVersion.Text.Trim();
            if (!string.IsNullOrEmpty(curType) && !string.IsNullOrEmpty(curVersion))
            {
                var curTypeVer = curType + GlobalVar.char_VersionSpilt + curVersion;
                if (GlobalVar.closeSave.CurrentTypeName == curTypeVer)
                {

                    SaveParamaToControl(GlobalVar.motionConfig);
                    displalyInfo.displayInfoMain("型号保存成功");
                }
                else
                {
                    displalyInfo.displayInfoMain($"已切换的配方{GlobalVar.closeSave.CurrentTypeName}与当前选中的配方{curTypeVer}不匹配  保存失败");
                }
            }
        }
        private VisionPos Get_VisionCenter()
        {

            var put = new VisionPos();
            put.X = (double)nud_VisionCenterX.Value;
            put.Y = (double)nud_VisionCenterY.Value;
            put.R = (double)nud_VisionCenterR.Value;

            return put;
        }

        private void hswc_Postion_Load(object sender, EventArgs e)
        {
 
                hswc_Postion.Parent.MouseWheel += my_MouseWheel_2;

                hswc_Postion.HMouseMove += new HMouseEventHandler(Image_MouseMove);
                hswc_Postion.HMouseDown += new HMouseEventHandler(Image_MouseDown);
                hswc_Postion.HMouseUp += new HMouseEventHandler(Image_MouseUp);
                GlobalVar.hwindow_cam_2_hwin = hswc_Postion.HalconWindow;
                hswc_Postion.HalconWindow.SetDraw("margin");
                hswc_Postion.SetFullImagePart();
            
        }
        private void my_MouseWheel_2(object sender, MouseEventArgs e)
        {
            try
            {
                Point pt = hswc_Postion.Location;
                int leftBorder = hswc_Postion.Location.X;
                int rightBorder = hswc_Postion.Location.X + hswc_Postion.Size.Width;
                int topBorder = hswc_Postion.Location.Y;
                int bottomBorder = hswc_Postion.Location.Y + hswc_Postion.Size.Height;

                if (e.X > leftBorder && e.X < rightBorder && e.Y > topBorder && e.Y < bottomBorder)
                {
                    MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, e.X - pt.X, e.Y - pt.Y, e.Delta);
                    hswc_Postion.HSmartWindowControl_MouseWheel(sender, newe);
                }
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain("鼠标滚轮操作异常 :" + ex.Message);
               
            }

        }
        private void Image_MouseMove(object sender, HMouseEventArgs e)
        {

            if (((HSmartWindowControl)sender).Name == "hswc_Postion")
            {
                tbx_pixX.Text = e.X.ToString();
                tbx_pixY.Text = e.Y.ToString();
                if (e.Button.ToString() == "Left" && e.Y > 0 && e.X > 0)
                {
                    HOperatorSet.GetImageSize(visionProc.srcObject_pos, out HTuple imgWidth, out HTuple imgHeight);
                    if (e.X <= imgWidth.D && e.Y <= imgHeight.D)
                    {
                        HOperatorSet.GetGrayval(visionProc.srcObject_pos, e.Y, e.X, out HTuple grayval);
                        tbx_grayVal.Text = grayval.I.ToString();
                    }
                }
                if (isDown1 && e.Button.ToString() == "Left" && visionProc.isReduce)
                {
                    point_move = new PointF((float)e.X, (float)e.Y);
                    HObject curRegion;
                    HOperatorSet.GenCircle(out curRegion, point_move.Y, point_move.X, visionProc.reduce_Radius);
                    if (!MaskRegion1.IsInitialized())
                        MaskRegion1 = curRegion;
                    else
                    {
                        HObject conObj;
                        HOperatorSet.ConcatObj(MaskRegion1, curRegion, out conObj);
                        MaskRegion1 = conObj;
                    }
                    HOperatorSet.Difference(visionProc.ROI1.ROI_Template, MaskRegion1, out HObject MaskResult);
                    visionProc.ROI1.ROI_Template = MaskResult;
                    ImageDisplay_1_objSimple(visionProc.ROI1.ROI_Template, "blue");
                }
            }
        
        }
        public void ImageDisplay_1_objSimple(HObject drawObject, string color)
        {
            try
            {
                if (InvokeRequired)
                {
                    hswc_Postion.Invoke(new MethodInvoker(() =>
                    {
                        hswc_Postion.HalconWindow.ClearWindow();
                        hswc_Postion.HalconWindow.DispObj(visionProc.srcObject_pos);
                        hswc_Postion.HalconWindow.SetColor(color);
                        hswc_Postion.HalconWindow.SetDraw("margin");
                        hswc_Postion.HalconWindow.DispObj(drawObject);

                    }));
                }
                else
                {
                    hswc_Postion.HalconWindow.ClearWindow();
                    hswc_Postion.HalconWindow.DispObj(visionProc.srcObject_pos);
                    hswc_Postion.HalconWindow.SetColor(color);
                    hswc_Postion.HalconWindow.SetDraw("margin");
                    hswc_Postion.HalconWindow.DispObj(drawObject);

                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("定位相机检测显示异常:" + ex.Message);
              
            }
        }
        PointF point_move;
        /// <summary>
        /// 涂抹区域
        /// </summary>
        HObject MaskRegion1 = new HObject();
        public void DrawRoiRegion1()
        {
            visionProc.DrawRoiRegion1(hswc_Postion);
        }
        public void ClearRoiRegion1()
        {
            visionProc.ClearROIRegion1();
        }
        public void ImageDisplay_1_obj(HObject measureImage, string color)
        {
            if (measureImage == null)
                measureImage = new HObject();
            hswc_Postion.BeginInvoke(new MethodInvoker(() =>
            {
                try
                {
                    hswc_Postion.HalconWindow.SetDraw("margin");
                    hswc_Postion.HalconWindow.SetColor(color);
                    measureImage.DispObj(hswc_Postion.HalconWindow);
                    hswc_Postion.SetFullImagePart();
                }
                catch (Exception ex)
                {
                    displalyInfo.displayInfoMain("定位相机显示异常:" + ex.Message);
                }
            }));
        }
        public void ImageDisplay_1(HObject measureImage)
        {
            try
            {
                hswc_Postion.BeginInvoke(new MethodInvoker(() =>
                {

                    hswc_Postion.HalconWindow.ClearWindow();
                    measureImage.DispObj(hswc_Postion.HalconWindow);
                    hswc_Postion.SetFullImagePart();
                }));
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("ImageDisplay_1:" + ex.Message);

            }
        }
        private void Image_MouseUp(object sender, HMouseEventArgs e)
        {
            if (isDown1)
            {
                isDown1 = false;
                hswc_Postion.HMoveContent = true;
            }
        }
        bool isDown1 = false;
        private void Image_MouseDown(object sender, HMouseEventArgs e)
        {
            if (!isDown1 && e.Button.ToString() == "Left" && visionProc.isReduce)
            {
                hswc_Postion.HMoveContent = false;
                isDown1 = true;
            }
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cbo_selectVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo_type_selectType.SelectedIndex > -1)
            {
                var strSelect = cbo_type_selectType.Text;
                var tList = GlobalVar.tv_list.Where(a => a.type == strSelect).ToList();
                if (tList.Count > 0)
                {
                    if (((ComboBox)sender).Name == "cbo_type_selectType")
                    {
                        var strVersion11 = cbo_selectVersion.Text;
                        var arrVer = GlobalVar.tv_list.Where(a => a.type == cbo_type_selectType.Text).Select(a => a.Version).Distinct().ToArray();
                        cbo_selectVersion.Items.Clear();
                        cbo_selectVersion.Items.AddRange(arrVer);
                        if (arrVer.Contains(strVersion11))
                            cbo_selectVersion.Text = strVersion11;
                        else
                            cbo_selectVersion.Text = "";
                    }
                }
            }

        }

        private void btn_changeModel_Click(object sender, EventArgs e)
        {
            var curType = cbo_type_selectType.Text.Trim();
            var curVersion = cbo_selectVersion.Text.Trim();
            if (string.IsNullOrEmpty(curType) || string.IsNullOrEmpty(curVersion))
            {
                MessageBox.Show("产品名称 产品型号 不存在");
                return;
            }
            var curlist = GlobalVar.tv_list.Where(a => a.type == curType && a.Version == curVersion).ToList();
            if (curlist.Count < 1)
            {
                MessageBox.Show("产品名称 产品型号 不存在");
                return;
            }
            if (curlist[0].GetTypeVersion() == GlobalVar.closeSave.CurrentTypeName)
            {
                MessageBox.Show($"当前即是产品{GlobalVar.closeSave.CurrentTypeName}   不用切换");
                return;
            }
            if (GlobalVar.isAutoRun)
            {
                MessageBox.Show("机器自动状态下不能切换产品");
                return;
            }
            try
            {
                PageVersionConfirm confirm = new PageVersionConfirm(curlist[0].GetTypeVersion());
                confirm.ShowDialog();
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain(ex.StackTrace);
            }
        }

        private void cbo_selectVersion_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void nud_Rmax_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            try
            {
                if (visionProc.MarkRegion==null || visionProc.srcObject_pos==null)
                {
                    MessageBox.Show("请创新创建ROI");
                    return;
                }

                HOperatorSet.ReduceDomain(visionProc.srcObject_pos, visionProc.MarkRegion, out HObject cutObj1);
                HOperatorSet.Threshold(cutObj1, out HObject region1, 200, 255);
                HOperatorSet.ClosingCircle(region1, out region1, 3);
                HOperatorSet.Connection(region1, out region1);
                HOperatorSet.SelectShapeStd(region1, out region1, "max_area", 70);
                HOperatorSet.AreaCenter(region1, out HTuple area1, out HTuple row_Cut1, out HTuple col_Cut1);
                hswc_Postion.HalconWindow.SetDraw("margin");
                hswc_Postion.SetFullImagePart();
                GlobalVar.imageDisplay1_obj(region1, "red");
                if (rbn_v_Plate1.Checked)
                {
                    if (visionProc.Mark1Pix == null)
                    {
                        return;
                    }

                    if (GlobalMotion.TipInfo($"平台1 当前数据，X={col_Cut1[0].D.ToString("F03")}，Y={row_Cut1[0].D.ToString("F03")}，是否更新？")!=1)
                    {
                        return;
                    }

                    nud_VisionCenterX.Value = (decimal)(col_Cut1[0].D - visionProc.Mark1Pix.X);
                    nud_VisionCenterY.Value = (decimal)(row_Cut1[0].D - (double)visionProc.Mark1Pix.Y);
                    HOperatorSet.GenCrossContourXld(out HObject CenterObj, row_Cut1, col_Cut1, 200, 0);
                    GlobalVar.imageDisplay1_obj(CenterObj, "red");
                }
                else
                {
                    if (visionProc.Mark2Pix == null)
                    {
                        return;
                    }

                    if (GlobalMotion.TipInfo($"平台2 当前数据，X={col_Cut1[0].D.ToString("F03")}，Y={row_Cut1[0].D.ToString("F03")}，是否更新？") != 1)
                    {
                        return;
                    }

                    nud_VisionCenterX.Value = (decimal)(col_Cut1[0].D - visionProc.Mark2Pix.X);
                    nud_VisionCenterY.Value = (decimal)(row_Cut1[0].D - (double)visionProc.Mark2Pix.Y);
                    HOperatorSet.GenCrossContourXld(out HObject CenterObj, row_Cut1, col_Cut1, 200, 0);
                    GlobalVar.imageDisplay1_obj(CenterObj, "red");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
